package com.example.springsecuritystart.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;

@RestController
public class UserController {

    @RequestMapping("/user")
    public void userInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        System.out.println("name = " + name);
        System.out.println("authorities = " + authorities);

        new Thread(() -> {
            Authentication authentication1 = SecurityContextHolder.getContext().getAuthentication();
            if (authentication1 == null) {
                System.out.println("获取用户信息失败");
                return;
            }
            final String name1 = authentication.getName();
            final Collection<? extends GrantedAuthority> authorities1 = authentication.getAuthorities();
            final String threadName = Thread.currentThread().getName();
            System.out.println(threadName + ":name = " + name1);
            System.out.println(threadName + ":authorities = " + authorities1);
        }).start();
    }

    @GetMapping("/user2")
    public void userInfo(HttpServletRequest req, HttpServletResponse resp) {
        final AsyncContext asyncContext = req.startAsync();
        CompletableFuture.runAsync(() -> {
            try {
                final PrintWriter writer = asyncContext.getResponse().getWriter();
                writer.write("Hello javaboy!");
                asyncContext.complete();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
    }

    @RequestMapping("/authentication")
    public void authentication(Authentication authentication) {
        System.out.println("authentication = " + authentication);
    }

    @RequestMapping("/principal")
    public void principal(Principal principal) {
        System.out.println("principal = " + principal);
    }

    @RequestMapping("/info")
    public void info(HttpServletRequest req) {
        final String remoteUser = req.getRemoteUser();
        Authentication authentication = (Authentication) req.getUserPrincipal();
        final boolean admin = req.isUserInRole("admin");
        System.out.println("remoteUser = " + remoteUser);
        System.out.println("auth.getName() = " + authentication.getName());
        System.out.println("admin = " + admin);
    }
}
